package com.mark.web.bill.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mark.common.Constant;
import com.mark.common.ServerResponse;
import com.mark.web.bill.entity.po.GoodsCategory;
import com.mark.web.bill.entity.vo.GoodsCategoryVo;
import com.mark.web.bill.enums.GoodsCategoryEnum;
import com.mark.web.bill.exception.BillException;
import com.mark.web.bill.mapper.GoodsCategoryMapper;
import com.mark.web.bill.service.IGoodsCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Description: 加工零件种类的业务实现
 * @Author: Mark
 * @CreateDate: 2019/2/16 21:18
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Service
public class GoodsCateServiceImpl implements IGoodsCateService {
    private final GoodsCategoryMapper goodsCategoryMapper;

    @Autowired
    public GoodsCateServiceImpl(GoodsCategoryMapper goodsCategoryMapper) {
        this.goodsCategoryMapper = goodsCategoryMapper;
    }

    //region 获取同一个父级的零件种类
    @Override
    public ServerResponse<List<GoodsCategoryVo>> getGoodsCateList(int parentId) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectSubGoodsCatesByParentId(parentId,null);
        List<GoodsCategoryVo> result = Lists.newArrayList();
        goodsCategoryList.forEach(goodsCategory -> result.add(new GoodsCategoryVo(goodsCategory)));
        return ServerResponse.createBySuccess(result);
    }
    //endregion

    //region 获取渲染Cascader组件的属性数据
    @Override
    public ServerResponse<List<Map<String, Object>>> getGoodsCateCascader() {
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectSubGoodsCatesByParentId(0,null);

        List<Map<String, Object>> result = buildGoodsCateCascader(0,goodsCategoryList);
        return ServerResponse.createBySuccess(result);
    }

    private List<Map<String, Object>> buildGoodsCateCascader(int parentId, List<GoodsCategory> goodsCategoryList) {
        List<Map<String, Object>> result = Lists.newArrayList();
        for(GoodsCategory goodsCategory : goodsCategoryList){
            if(goodsCategory.getParentId() == parentId){
                Map<String, Object>  childrenMap = Maps.newHashMap();
                childrenMap.put("value", goodsCategory.getId());
                childrenMap.put("label", goodsCategory.getGoodsCategoryName());
                List<GoodsCategory> subGoodsCategoryList = goodsCategoryMapper.selectSubGoodsCatesByParentId(goodsCategory.getId(),null);
                if(subGoodsCategoryList != null && subGoodsCategoryList.size() > 0){
                    List<Map<String, Object>> children = buildGoodsCateCascader(goodsCategory.getId(), subGoodsCategoryList);
                    childrenMap.put("children", children);
                }
                result.add(childrenMap);
            }
        }
        return result;
    }
    //endregion

    //region 新增一条种类记录
    @Override
    public ServerResponse<String> add(GoodsCategory goodsCategory) {
        //状态默认为可用
        goodsCategory.setEnableStatus(GoodsCategoryEnum.AVAILABLE.getState());

        //层级属性的设置:如果父级ID为0，则level为1。否则取父级种类的level+1
        setCategoryLevel(goodsCategory);

        //插入数据库
        int insert = goodsCategoryMapper.insert(goodsCategory);
        if(insert != Constant.SUCCESS){
            return ServerResponse.createByErrorMessage("新增种类失败！");
        }
        return ServerResponse.createBySuccessMessage("新增种类成功！");
    }
    //endregion

    //region 根据父级种类ID和种类名称模糊搜索
    @Override
    public ServerResponse<List<GoodsCategoryVo>> getSearchGoodsCates(Integer parentId, String categoryName) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectSubGoodsCatesByParentId(parentId, categoryName);
        List<GoodsCategoryVo> result = Lists.newArrayList();
        goodsCategoryList.forEach(goodsCategory -> result.add(new GoodsCategoryVo(goodsCategory)));
        return ServerResponse.createBySuccess(result);
    }
    //endregion

    //region 编辑种类
    @Override
    public ServerResponse<String> edit(GoodsCategory goodsCategory) {
        //层级属性的设置:如果父级ID为0，则level为1。否则取父级种类的level+1
        setCategoryLevel(goodsCategory);
        int update = goodsCategoryMapper.updateByPrimaryKeySelective(goodsCategory);
        if(update != Constant.SUCCESS){
            return ServerResponse.createByErrorMessage("更新种类失败！");
        }
        return ServerResponse.createBySuccessMessage("更新种类成功！");
    }
    //endregion

    /*
     * ------------------------------------------- 公用私用方法抽取 --------------------------------------------------------------------
     */
    //region 新增或者编辑时种类等级的设置
    /**
     * 层级属性的设置:如果父级ID为0，则level为1。否则取父级种类的level+1
     * @param goodsCategory ：
     */
    private void setCategoryLevel(GoodsCategory goodsCategory) {
        if(goodsCategory.getParentId() == 0 || goodsCategory.getParentId()==null){
            goodsCategory.setLevel(1);
        }else{
            GoodsCategory parentCate = goodsCategoryMapper.selectByPrimaryKey(goodsCategory.getParentId());
            if(parentCate == null){
                throw new BillException("父级种类不存在！");
            }
            goodsCategory.setLevel(parentCate.getLevel());
        }
    }
    //endregion
}
